
# include "dfxRenderState_OGL.h"
# include "dfxGraphicsDevice_OGL.h"
# include "dfxLog.h"

namespace DFX
{
    AlphaState_OGL::AlphaState_OGL(GraphicsDevice_OGL *gd, const AlphaStateDescription &desc)
        : AlphaState(desc), mGraphicsDevice(gd)
    {
        BlendEnabled        = desc.BlendEnabled;
        AlphaTestEnabled    = desc.AlphaTestEnabled;
        SrcBlend            = GraphicsDevice_OGL::BlendOption[desc.SourceBlend];
        DestBlend           = GraphicsDevice_OGL::BlendOption[desc.DestinationBlend];
        AlphaFunction       = GraphicsDevice_OGL::CompareFunction[desc.AlphaFunction];
        BlendEquation       = GraphicsDevice_OGL::BlendEquation[desc.BlendEquation];
        Reference           = desc.Reference;
        ConstantColor[0]    = desc.ConstantColor.R;
        ConstantColor[1]    = desc.ConstantColor.G;
        ConstantColor[2]    = desc.ConstantColor.B;
        ConstantColor[3]    = desc.ConstantColor.A;
    }

    AlphaState_OGL::~AlphaState_OGL()
    {
    }
    
    void AlphaState_OGL::Apply()
    {
        mGraphicsDevice->SetAlphaState(this);
    }

    RasterizerState_OGL::RasterizerState_OGL(GraphicsDevice_OGL *gd, const RasterizerStateDescription &desc)
        : RasterizerState(desc), mGraphicsDevice(gd)
    {
        Enabled     = (desc.CullMode != CULL_MODE_NONE);
        Face        = (desc.CullMode == CULL_MODE_CLOCKWISE) ? GL_CW : GL_CCW;
        PolygonMode = GraphicsDevice_OGL::PolygonMode[desc.FillMode];
    }

    RasterizerState_OGL::~RasterizerState_OGL()
    {
    }
    
    void RasterizerState_OGL::Apply()
    {
        mGraphicsDevice->SetRasterizerState(this);
    }

    DepthState_OGL::DepthState_OGL(GraphicsDevice_OGL *gd, const DepthStateDescription &desc)
        : DepthState(desc), mGraphicsDevice(gd)
    {
        Enabled         = desc.Enabled;
        Writable        = desc.Writable;
        CompareFunction = GraphicsDevice_OGL::CompareFunction[desc.CompareFunction];
    }

    DepthState_OGL::~DepthState_OGL()
    {
    }
        
    void DepthState_OGL::Apply()
    {
        mGraphicsDevice->SetDepthState(this);
    }

    StencilState_OGL::StencilState_OGL(GraphicsDevice_OGL *gd, const StencilStateDescription &desc)
        : StencilState(desc), mGraphicsDevice(gd)
    {
        Enabled         = desc.Enabled;
        Face            = GraphicsDevice_OGL::StencilFace[desc.Face];
        CompareFunction = GraphicsDevice_OGL::CompareFunction[desc.CompareFunction];
        Reference       = desc.Reference;
        Mask            = desc.Mask;
        WriteMask       = desc.WriteMask;
        OnFail          = GraphicsDevice_OGL::StencilOption[desc.OnFail];
        OnZFail         = GraphicsDevice_OGL::StencilOption[desc.OnZFail];
        OnZPass         = GraphicsDevice_OGL::StencilOption[desc.OnZPass];
    }

    StencilState_OGL::~StencilState_OGL()
    {
    }

    void StencilState_OGL::Apply()
    {
        mGraphicsDevice->SetStencilState(this);
    }
}